<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%--
  Created by IntelliJ IDEA.
  User: qupen
  Date: 2016/10/12
  Time: 9:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="../../include/head.jsp">
    <jsp:param name="title" value="机床设置"/>
</jsp:include>

<jsp:include page="../../include/tenant_header.jsp"/>

<jsp:include page="../../include/tenant_sidebar.jsp"/>
<div class="content-wrapper content-wrapper-bg">
    <section class="content-header">
        <h4 class="page-title" style="display: inline-block"><spring:message code="label.tenant.device.list"/> </h4>
        <a class="btn btn-add" href="javascript:getCreateDevicePage();" style="margin-left:44px;">
            <i class="fa fa-plus" style="margin-right: 3px"></i><spring:message code="label.tenant.register.device"/>
        </a>
        <a class="btn btn-add" id="export" href="javascript:;" style="margin-left:44px;">
            <i class="fa fa-plus" style="margin-right: 3px"></i><spring:message code="label.tenant.export.all.device"/>
        </a>
        <div class="form-group has-success has-feedback" style="float: right;margin-right: 30px;vertical-align: middle;top: 5px;">
            <input type="text" placeholder='<spring:message code="label.admin.search"/>' class="form-control" id="inputSuccess2" style="border-radius:5px;border-color:#aabfcb">
            <span class="glyphicon glyphicon-search form-control-feedback"></span>
        </div>
    </section>
    <section class="content">
        <div class="box box-primary table-header-line">
            <div class="box-body"  style="padding: 0">
                <table class="table" id="device_list">
                    <thead>
                    <tr>
                        <th><spring:message code="label.device.id"/></th>
                        <th><spring:message code="label.device.type"/></th>
                        <th><spring:message code="label.device.model"/></th>
                        <th><spring:message code="label.tenant.create.description"/></th>
                        <th><spring:message code="label.tenant.children.state"/></th>
                        <th><spring:message code="label.operation"/></th>
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
        <%--</div>--%>
    </section>
</div>
<jsp:include page="../../include/foot.jsp"/>

<script>
    $(function () {
        var table = $('#device_list').DataTable({
            "dom": '<"datatable-header"<"dataTables_filter">l><"datatable-scroll"t><"datatable-footer dataTable-footer-padding"<"pull-left"i><"pull-right"p>>',
            paging: true,
            lengthChange: false,
            ordering: true,
            autoWidth: false,
            info: true,
            serverSide: true,
            fixedHeader: true,
            searching: true,
            aLengthMenu: [15],
            ajax: {
//                url: '/partner/{tenant}/api/v1/device/devices'.replace("{tenant}", getKeyByUrl()),
                url: '/partner/{tenant}/api/v1/device/devicesByDataTable'.replace("{tenant}", getKeyByUrl()),
                dataSrc: 'data'
            },
            columns: [
                {
                    data: 'device_unique_id'
                },
                {
                    data: 'device_type'
                },
                {
                    data: 'device_model'
                },
                {
                    data: 'description'
                },
                {
                    data: 'state',
                    "render": function (data, type, full, meta) {
                        if (data == 0||data==null) {
                            return "<spring:message code="label.no.check.heart"/>";
                        } else if (data == 1) {
                            return "<spring:message code="label.normal.heart"/>";
                        }
                    }
                },
                {
                    data: 'device_id',
                    "render": function (data, type, full, meta) {

                        var divString = '<div class="sie-btn-group">' +
                                '<a href="javascript:;" onclick="editDevice(\''+'/partner/{tenant}/device/edit?deviceId=' + data + '#deviceSetting\')"><i class="sie-btn-edit"></i><span><spring:message code="label.user.edit"/></span></a>' +
                                '<a href="javascript:;" onclick="deleteDevice(\'' + data + '\')"><i class="sie-btn-delete"></i><span><spring:message code="label.user.delete"/></span></a>' +
                                '<a href="javascript:;" onclick="planDowntimeSetting(\'' + data + '\')"><i class="sie-btn-setting"></i><span><spring:message code="label.tenant.setting.plan.downtime"/></span></a>' +
                                '</div>';
                        return divString;
                    }
                }
            ],
            "language": {url: '/lang/datatable.chs.json'}
        });
        $('#inputSuccess2').bind('keyup', function (e) {
            table.search(this.value).draw();
        });

        $("#export").click(function(){
            BYLoadingToast('<spring:message code="label.loading"/>', function (dtd) {
                $.ajax({
                    url: "/partner/{tenant}/api/v1/device/exportDeviceXml".replace("{tenant}", getKeyByUrl()),
                    type: "get",
                    success: function (result) {
                        if (result.data == null || result.data == "" || result.data == undefined) {
                            dtd.resolve();
                        } else {
                            dtd.resolve();
                            window.location.href = "/api/v1/deviceXml/get?name=" + result.data;
                        }
                    },
                    error: function (e) {
                        dtd.resolve();
                        console.log(e);
                    }
                });
            });
        });
    });

    var operation = function (value, _this) {
        console.log(_this);
        if (value.indexOf("?") != -1) {
            window.location.href = value.replace("{tenant}", getKeyByUrl());
        } else {
            var action = value.split("=")[0];
            var id = value.split("=")[1];
            if (action == "delete") {
                var resetArr = ["", '<spring:message code="label.sure.delete"/>', "/partner/{tenant}/api/v1/device/delete"];
                var resetAfterArr = ["", '<spring:message code="label.success.operation"/>'];
                deleteArticle(id, resetArr, resetAfterArr, action, _this);
            }
        }
    };


    function deleteArticle(id, arr, afterArr, action, _this) {
        swal({
            title: arr[0],
            text: arr[1],
            type: "warning",
            showCancelButton: "true",
            showConfirmButton: "true",
            confirmButtonText: '<spring:message code="label.admin.confirm"/>',
            cancelButtonText: '<spring:message code="label.user.cancel"/>',
            animation: "slide-from-top"
        }, function (isConfirm) {
            if (isConfirm) {
                $.ajax({
                    type: "post",
                    url: arr[2].replace("{tenant}", getKeyByUrl()),
                    data: {"deviceId": id},
                    success: function () {
                        var table = $('#device_list').DataTable();
                        table.ajax.reload();
                    },
                    fail: function () {
                        swal("Error!", "", "error");
                    }
                });
            } else {
                $(_this).find("option:first").attr("selected", true);
                console.log("hehehehehehe!");
            }
        });
    }

    function getCreateDevicePage() {
        var url = "/partner/{tenant}/tenant/device/create#deviceSetting";
        window.location.href = url.replace("{tenant}", getKeyByUrl());
    }

    function editDevice(value){
        window.location.href=value.replace("{tenant}",getKeyByUrl());
    }

    function deleteDevice(deviceId) {
        console.log("del:" + deviceId);
        swal({
            title: '<spring:message code="label.tenant.delete.device"/>',
            text: '<spring:message code="label.sure.delete"/>',
            type: "warning",
            showCancelButton: "true",
            showConfirmButton: "true",
            confirmButtonText: '<spring:message code="label.admin.confirm"/>',
            cancelButtonText: '<spring:message code="label.user.cancel"/>',
            animation: "slide-from-top"
        }, function () {
            $.ajax({
                type: "post",
                url: "/partner/{tenant}/api/v1/device/delete".replace("{tenant}",getKeyByUrl()),
                data: {deviceId:deviceId,enabled:-1},
                success: function (data) {
                    if (data.data !== null && data.data != "" && data.data != undefined) {
                        alert(data.data);
                    }
                    var table = $('#device_list').DataTable();
                    table.ajax.reload();
                },
                error: function (e) {
                    console.log(e);
                    swal("Error!", "", "error");
                }
            });
        });
    }

    function shiftSetting(deviceId){
        window.location.href=("/partner/{tenant}/shift/list?deviceId="+deviceId+"#deviceSetting").replace("{tenant}",getKeyByUrl());
    }

    function planDowntimeSetting(deviceId){
        window.location.href=("/partner/{tenant}/planDowntime/list?deviceId="+deviceId+"#deviceSetting").replace("{tenant}",getKeyByUrl());
    }
</script>
